//
//  UIViewController+Extension.swift
//  AIPhotoProcess
//
//  Created by aw031 on 17/10/2025.
//

import Foundation
import UIKit

extension UIViewController {
    
    
    func popToViewController(with viewControllerClass: UIViewController.Type) {
        for controller in self.navigationController?.viewControllers ?? [] {
            if controller.isKind(of: viewControllerClass) {
                self.navigationController?.popToViewController(controller, animated: true)
                return
            }
        }
        self.navigationController?.popViewController(animated: true)
    }

    
    static func getCurrentShowTopVC() -> UIViewController? {
        guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
              let window = KWindow else { return nil }
        
        var topVC = window.rootViewController
        while let presentedVC = topVC?.presentedViewController {
            topVC = presentedVC
        }
        
        if let navVC = topVC as? UINavigationController {
            topVC = navVC.visibleViewController
        }
        
        if let tabVC = topVC as? UITabBarController {
            topVC = tabVC.selectedViewController
        }
        
        return topVC
    }
    
    
    static func findViewControllerInTransitionView(_ view: UIView) -> UIViewController? {
        // 遍历子视图，寻找控制器
        for subview in view.subviews {
            if let nextResponder = subview.next {
                if let viewController = nextResponder as? UIViewController {
                    return viewController
                }
            }
            
            // 递归查找
            if let foundVC = findViewControllerInTransitionView(subview) {
                return foundVC
            }
        }
        return nil
    }

    static func getTopViewControllerFromTransitionViews() -> UIViewController? {
        guard let keyWindow = UIApplication.shared.windows.first(where: { $0.isKeyWindow }) else {
            return nil
        }
        // 查找所有的 UITransitionView
        let transitionViews = keyWindow.subviews.compactMap { subview -> UIView? in
            if String(describing: type(of: subview)) == "UITransitionView" {
                return subview
            }
            return nil
        }
        // 按视图层级排序，找到最上面的
        let topTransitionView = transitionViews.max(by: {
            $0.frame.origin.y < $1.frame.origin.y
        })
        if let topView = topTransitionView {
            return findViewControllerInTransitionView(topView)
        }
        
        return nil
    }
    
    
    
    func pushSettingViewController() {
        let settiingVC = SettingViewController.init()
        self.navigationController?.pushViewController(settiingVC, animated: true)
    }
    
    func pushSettingSendFeedbackViewController() {
        let sendFeedbackVC = SettingSendFeedbackVC.init()
        self.navigationController?.pushViewController(sendFeedbackVC, animated: true)
    }
    
}
